//
//  CYFreeTrialViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/6.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYFreeTrialViewController.h"
#import "CYTextField.h"
@interface CYFreeTrialViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) CYView *line;
@property (nonatomic, strong) CYTextField *userName;
@property (nonatomic, strong) CYTextField *passWord;
@property (nonatomic, strong) CYButton *loginBtn;
@property (nonatomic, strong) CYLabel *promptLBL;
@property (nonatomic, strong) CYLabel *prompt_contentLBL;
@property (nonatomic, copy) NSString *content;

@end

@implementation CYFreeTrialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.userName.hidden = self.passWord.hidden = self.loginBtn.hidden = self.promptLBL.hidden = self.prompt_contentLBL.hidden = self.line.hidden =  NO;
    // Do any additional setup after loading the view.
}
/*懒加载*/
- (CYTextField *)userName{
    if (!_userName) {
        _userName = [[CYTextField alloc]initWithLeftImage:@"user_login" Placeholder:@"请输入用户名"];
        _userName.text = @"Guesu00000";
        _userName.userInteractionEnabled = NO;
        [self.view addSubview:_userName];
        
        WS(self)
        [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.view).with.offset(AL(PT(60)));
            make.top.equalTo(weakSelf.view).with.offset(NAVIBAR_HEIGHT + STATUS_HEIGHT);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - AL(PT(60 * 2)), AL(PT(120))));
        }];
    }
    
    return _userName;
}
- (CYTextField *)passWord{
    if (!_passWord) {
        _passWord = [[CYTextField alloc]initWithLeftImage:@"lock" Placeholder:@"请输入密码"];
        _passWord.keyboardType = UIKeyboardTypeASCIICapable;
        _passWord.detail_placeholder_size = AL(PT(22));
        _passWord.detail_placeholder_color = RGBA(210, 210, 215, 1);
        _passWord.detail_placeholder = @"(6-12位字母或数字组合)";
        _passWord.returnKeyType = UIReturnKeyNext;
        _passWord.delegate = self;
        [self.view addSubview:_passWord];
        
        WS(self)
        [_passWord mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.view).with.offset(AL(PT(60)));
            make.top.equalTo(_userName.mas_bottom).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - AL(PT(60 * 2)), AL(PT(120))));
        }];
    }
    
    return _passWord;
}
- (CYButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [[CYButton alloc]initWithTitle:@"登录" titleColor:[UIColor lightGrayColor] backgroundColor:ColorHex(0xf4333c) cornerRadius:5];
        [_loginBtn addTarget:self action:@selector(actionForloginBtn:) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(40))];
        [self.view addSubview:_loginBtn];
        
        WS(self)
        [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_passWord.mas_bottom).with.offset( AL(PT(120)));
            make.centerX.equalTo(weakSelf.view);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - AL(PT(60 * 2)), AL(PT(80))));
        }];
    }
    
    return _loginBtn;
}

- (CYLabel *)promptLBL{
    if (!_promptLBL) {
        _promptLBL = [[CYLabel alloc]initWithText:@"温馨提示:" size:AL(PT(28)) textcolor:[UIColor blackColor]];
        [self.view addSubview:_promptLBL];
        
        WS(self)
        [_promptLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.view).with.offset(AL(PT(60)));
            make.top.equalTo(_loginBtn.mas_bottom).with.offset(AL(PT(30)));
            make.size.mas_equalTo(_promptLBL.bounds.size);
        }];
    }
    
    return _promptLBL;
}

- (CYLabel *)prompt_contentLBL{
    
    if (!_prompt_contentLBL) {
        [self setupContent];
        _prompt_contentLBL = [[CYLabel alloc]initWithText:_content size:AL(PT(22)) textcolor:ColorHex(0x888888)];
        _prompt_contentLBL.numberOfLines = 0;
        [self.view addSubview:_prompt_contentLBL];
        
        WS(self)
        [_prompt_contentLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.view).with.offset(AL(PT(64)));
            make.top.equalTo(_promptLBL.mas_bottom).with.offset(AL(PT(20)));
            make.size.mas_equalTo([_prompt_contentLBL autoSizeWithWidth:SCREEN_WIDTH - AL(PT(60 *2))]);
        }];
    }
    
    return _prompt_contentLBL;
    
}

- (CYView *)line{
    if (!_line) {
        _line = [[CYView alloc]initWithColor:[UIColor lightGrayColor]];
        [self.view addSubview:_line];
        
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(NAVIBAR_HEIGHT + STATUS_HEIGHT , 0, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - 0.5, 0));
        }];
    }
    
    return _line;
}

//登录事件
- (void)actionForloginBtn:(CYButton *)sender{
    //判断任意的输入框是否都有值
    if (![self judgeTextLength]){
        [CYAlert showWithTitle:@"错误" message:@"请输入密码" inController:self complete:^{}];
        return;
    }
    
    NSDictionary *dict = @{@"username":_userName.text, @"password":_passWord.text, @"reg_ip":[CYCache getIP]};
    
    //网络请求
 self.task = [CYHttps POST:API_freeLogin parameters:dict showHUD:YES progress:nil success:^(id data) {
     if (data)//登录成功
     {
         [CYUser shareUser].token = data;
         [CYUser shareUser].username = _userName.text;
         [[AppDelegate shareAppDelegate] initializeHuanXinSDKWithApplication:nil didFinishLaunchingWithOptions:nil] ;
         Class vc = NSClassFromString(@"CYTabBarController");
         UITabBarController *tabVC = (UITabBarController *)[vc new];
         tabVC.selectedIndex = 0;
         [[UIApplication sharedApplication] keyWindow].rootViewController = tabVC;
         
     }
        
    } failure:nil];
    
}

//试玩用户生成网络请求
- (void)https{
    
 self.spareTask = [CYHttps POST:API_freeName parameters:nil showHUD:NO progress:nil success:^(id data) {
        if (data)
            _userName.text = data;
    } failure:nil];
    
}

//返回到首页
- (void)backToFirst{
  
    
    
}
//判定所有的textfiled的text是否为非空
- (BOOL)judgeTextLength{
    NSArray *array = [self.view subviews];
    BOOL isLength = YES;
    for (int i = 0; i < [array count]; i ++) {
        if ([array[i] isKindOfClass:[UITextField class]]) {
            UITextField *sub_textField = array[i];
            isLength = (sub_textField.text.length <= 0)?NO:isLength;
        }
    }
    return isLength;
}

- (void)setupContent{
    _content = @"1. 每个试玩账号初始金额为2000RMB，不允许充值。\n2. 每个IP每天允许有3个试玩账号。\n3. 每个试玩账号从注册开始，有效时间为72小时，超过时间系统自动收回。\n4. 试玩账号仅供玩家熟悉游戏，不能充值提款。\n5. 试玩账号不享有参加优惠活动的权利。\n6. 试玩账号的赔率仅供参考，可能与正式账号赔率不相符。\n7. 其他未说明事项以本公司解释为准。";
}

#pragma mark uitextfield代理
- (void)textFieldDidEndEditing:(UITextField *)textField{
    //当四个textfield全部有值，注册按钮文字高亮，表示可以点击
    if (_userName.text.length > 0 && _passWord.text.length > 0)
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    else
        [_loginBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    CYTextField *new_textfield = (CYTextField *)textField;
    if (new_textfield.text.length <= 0) {
        [new_textfield hiddenDetailPlaceholderLBL:NO];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    BOOL length_bool =[self judgeTextLength];
    UIColor *color = (length_bool && (string.length > 0 || textField.text.length > 1))?[UIColor whiteColor]:[UIColor lightGrayColor];
    [_loginBtn setTitleColor:color forState:UIControlStateNormal];
    return YES;
}

/*编辑时候，隐藏textfield的小文字*/
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    CYTextField *new_textfield = (CYTextField *)textField;
    [new_textfield hiddenDetailPlaceholderLBL:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}

- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:[UIColor whiteColor]];
    self.navigationItem.title = @"免费试玩";
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self https];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
